home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 24 / CU Amiga Magazine's Super CD-ROM 24 (1998)(EMAP Images)(GB)(Track 1 of 2)[!][issue 1998-07].iso / CUCD / Programming / SWI / source / lib / shlib.pl < prev    next >
Encoding:
Text File  |  1997-05-06  |  5.0 KB  |  181 lines

  1. /*  $Id: shlib.pl,v 1.6 1997/05/06 07:28:34 jan Exp $
  2.  
  3.     Copyright (c) 1990 Jan Wielemaker. All rights reserved.
  4.     jan@swi.psy.uva.nl
  5.  
  6.     Purpose: Front-end for loading shared libraries
  7. */
  8.  
  9. :- module(shlib,
  10.       [ load_foreign_library/1,    % :LibFile
  11.         load_foreign_library/2,    % :LibFile, +InstallFunc
  12.         unload_foreign_library/1,    % +LibFile
  13.         unload_foreign_library/1,    % +LibFile, +UninstallFunc
  14.         current_foreign_library/2,    % ?LibFile, ?Public
  15.         reload_foreign_libraries/0
  16.       ]).
  17.  
  18. :- module_transparent
  19.     load_foreign_library/1,
  20.     load_foreign_library/2.
  21.  
  22. :- dynamic
  23.     current_library/6,        % Lib x Entry x Path x
  24.                     % Module x Handle x Public
  25.     fpublic/1.
  26. :- volatile
  27.     current_library/6,
  28.     fpublic/1.
  29.  
  30.  
  31. :- (   (feature(dll, true) ; feature(open_shared_object, true))
  32.    ->  true
  33.    ;   format(user_error,
  34.           'library(shlib): warning: need .dll or .so based interface',
  35.           [])
  36.    ).
  37.  
  38.          /*******************************
  39.          *         DISPATCHING    *
  40.          *******************************/
  41.  
  42. open_goal(Lib, Handle, open_dll(Lib, Handle)) :-
  43.     feature(dll, true), !.
  44. open_goal(Lib, Handle, open_shared_object(Lib, Handle)) :-
  45.     feature(open_shared_object, true).
  46.  
  47. call_goal(Handle, Function, call_dll_function(Handle, Function)) :-
  48.     feature(dll, true), !.
  49. call_goal(Handle, Function, call_shared_object_function(Handle, Function)) :-
  50.     feature(open_shared_object, true), !.
  51.  
  52. close_goal(Handle, close_dll(Handle)) :-
  53.     feature(dll, true), !.
  54. close_goal(Handle, close_shared_object(Handle)) :-
  55.     feature(open_shared_object, true), !.
  56.  
  57. extensions(['.so']) :-
  58.     feature(open_shared_object, true), !.
  59. extensions(['.dll']) :-
  60.     feature(dll, true).
  61.  
  62. find_library(Spec, Lib) :-
  63.     extensions(Exts),
  64.     absolute_file_name(Spec,
  65.                [ extensions(Exts),
  66.                  access(read)
  67.                ], Lib), !.
  68. find_library(Spec, Spec) :-
  69.     atom(Spec), !.            % use machines finding schema
  70. find_library(foreign(Spec), Spec) :-
  71.     atom(Spec), !.            % use machines finding schema
  72. find_library(Spec, _) :-
  73.     '$warning'('load_foreign_library/1: Cannot find ~w: no such file',
  74.            [Spec]),
  75.     fail.
  76.  
  77.  
  78.          /*******************************
  79.          *        (UN)LOADING        *
  80.          *******************************/
  81.  
  82. load_foreign_library(Library) :-
  83.     load_foreign_library(Library, install).
  84.  
  85. load_foreign_library(LibFileSpec, Entry) :-
  86.     '$strip_module'(LibFileSpec, Module, LibFile),
  87.     load_foreign_library(LibFile, Module, Entry).
  88.  
  89. load_foreign_library(LibFile, _Module, Entry) :-
  90.     current_library(LibFile, Entry, _, _, _, _), !.
  91. load_foreign_library(LibFile, Module, Entry) :-
  92.     find_library(LibFile, Path),
  93.     open_goal(Path, Handle, OpenGoal),
  94.     OpenGoal,
  95.     (   clean_fpublic,        % safety
  96.         call_goal(Handle, Entry, CallGoal),
  97.         Module:CallGoal
  98.     ->  assert_shlib(LibFile, Entry, Path, Module, Handle),
  99.         clean_fpublic
  100.     ;   '$warning'('~w: failed to call entry point ~w', [LibFile, Entry]),
  101.         close_goal(Handle, CloseGoal),
  102.         CloseGoal,
  103.         fail
  104.     ).
  105.  
  106. unload_foreign_library(LibFile) :-
  107.     unload_foreign_library(LibFile, uninstall).
  108.  
  109. unload_foreign_library(LibFile, Uninstall) :-
  110.     current_library(LibFile, _, _, _, Public, Handle),
  111.     retractall(current_library(LibFile, _, _, _, _, _)),
  112.     call_goal(Handle, Uninstall, CallGoal),
  113.     ignore(CallGoal),
  114.     forall(member(Module:Head, Public),
  115.            (   functor(Head, Name, Arity),
  116.            abolish(Module:Name, Arity)
  117.            )),
  118.     close_goal(Handle, CloseGoal),
  119.     CloseGoal.
  120.         
  121. :- system:asserta(('$foreign_registered'(M, H) :-
  122.               shlib:foreign_registered(M, H))).
  123.  
  124. foreign_registered(M, H) :-
  125.     assert(fpublic(M:H)).
  126.  
  127. clean_fpublic :-
  128.     retractall(fpublic(_)).
  129.  
  130. assert_shlib(File, Entry, Path, Module, Handle) :-
  131.     findall(P, fpublic(P), Public),
  132.     retractall(current_library(File, _, _, _, _, _)),
  133.     asserta(current_library(File, Entry, Path, Module, Public, Handle)).
  134.  
  135.  
  136.          /*******************************
  137.          *     ADMINISTRATION        *
  138.          *******************************/
  139.  
  140. %    current_foreign_library(?File, ?Public)
  141. %
  142. %    Query currently loaded shared libraries
  143.  
  144. current_foreign_library(File, Public) :-
  145.     current_library(File, _Entry, _Path, _Module, Public, _Handle).
  146.  
  147.          /*******************************
  148.          *          RELOAD        *
  149.          *******************************/
  150.  
  151. %    reload_foreign_libraries
  152. %
  153. %    Reload all foreign libraries loaded (after restore of state)
  154.  
  155. reload_foreign_libraries :-
  156.     forall(retract(current_library(File, Entry, _, Module, _, _)),
  157.            (   load_foreign_library(File, Module, Entry)
  158.            ->  true
  159.            ;   '$warning'('reload_foreign_libraries/0: failed to load ~w',
  160.                      [File])
  161.            )).
  162.  
  163.  
  164.          /*******************************
  165.          *     CLEANUP (WINDOWS ...)    *
  166.          *******************************/
  167.  
  168. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  169. Called from Halt() in pl-os.c (if it  is defined), *after* all at_halt/1
  170. hooks have been executed, and after   dieIO(),  closing and flushing all
  171. files has been called.
  172.  
  173. On Unix, this is not very useful, and can only lead to conflicts.
  174. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  175.  
  176. unload_all_foreign_libraries :-
  177.     feature(unix, true), !.
  178. unload_all_foreign_libraries :-
  179.     forall(current_foreign_library(File, _),
  180.            unload_foreign_library(File)).
  181.